<template>
  <mu-row style="width: 100%">
    <mu-flex style="width: 100%" v-for="(item,index) in list" :key="index" justify-content="center" fill>
      <mu-flex>
        <span class="padr10">{{$com.isbool(labels[index])?labels[index]:''}}</span>
      </mu-flex>
      <mu-flex fill>
        <mu-text-field v-model="list[index]" full-width></mu-text-field>
      </mu-flex>
      <mu-flex v-if="$com.isbool(enableMod)">
        <mu-button color="primary" icon small @click="deleteOne(index)">
          <mu-icon value="close"></mu-icon>
        </mu-button>
        <mu-button color="primary" icon small @click="addOne()">
          <mu-icon value="add"></mu-icon>
        </mu-button>
      </mu-flex>
    </mu-flex>
  </mu-row>
</template>

<script>
  export default {
    name: 'arrayText',
    props: {
      data: '',
      labels: '',
      enableMod: ''
    },
    data () {
      return {
        list: []
      }
    },
    mounted () {
      if (typeof this.data !== 'object') {
        console.log(this.data)
        this.list.push(this.data)
      } else {
        this.list = this.data
      }
      console.log(this.list)
    },
    methods: {
      addOne () {
        this.list.push('')
        // this.data = this.list
        this.$emit('update:data', this.list)
      },
      deleteOne (index) {
        if (this.list.length === 1) {
          this.$toast.error('第一项无法删除！')
          return
        }
        this.list = arrayDelIndex(this.list, index)
        // this.data = this.list
        this.$emit('update:data', this.list)
      }

    }
  }

  function arrayDelIndex (array, index) {
    var tmp = []
    array.map(function (data, i) {
      if (i != index) {
        tmp.push(data)
      }
    })
    return tmp
  }
</script>

<style scoped>

</style>
